# -*- coding: utf-8 -*- 

#@PydevCodeAnalysisIgnore


#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

if request.env.web2py_runtime_gae:            # if running on Google App Engine
    db = DAL('gae')                           # connect to Google BigTable
    session.connect(request, response, db = db) # and store sessions and tickets there
    ### or use the following lines to store sessions in Memcache
    # from gluon.contrib.memdb import MEMDB
    # from google.appengine.api.memcache import Client
    # session.connect(request, response, db = MEMDB(Client()))
else:                                         # else use a normal relational database
    db = DAL('sqlite://storage.sqlite')       # if not, use SQLite or other DB
## if no need for session
# session.forget()

#########################################################################
## Here is sample code if you need for 
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################

global DEBUG
DEBUG = True
if DEBUG:
    # Relaod the module to get the changes:
    import uc_web2py_utils.i18n
    uc_web2py_utils.i18n = reload(uc_web2py_utils.i18n)
from gluon.tools import *
from uc_web2py_utils.i18n import fr_en_language_set
mail = Mail()                                  # mailer
auth = Auth(globals(),db)                      # authentication/authorization

# Set the default default for the table contributors:
DEFAULT_LANGUAGE = "fr"
DEFAULT_CITY = "Montreal"
DEFAULT_STATE = "Quebec"
DEFAULT_COUNTRY = "Canada" 

# Set common field size:
LANGUAGE_SIZE = 2
CITY_SIZE = 40
STATE_SIZE = 40
COUNTRY_SIZE = 40

# Customs fields for auth_user:
db.define_table(
    auth.settings.table_user_name,
    Field('first_name', length=128, default=None, required=True,
          label=T("First Name")),
    Field('last_name', length=128, default=None, required=True,
          label=T("Last Name")),
    Field('email', length=128, default=None, unique=True, label=T("Email")),
    Field('password', 'password', length=512,
          readable=False, label=T('Password')),
    Field("default_language", length=LANGUAGE_SIZE, 
          default=DEFAULT_LANGUAGE,
          required=True, requires=IS_IN_SET(fr_en_language_set(T), zero=None), 
          notnull=True, label=T("Default Language")),
    Field("default_city", length=CITY_SIZE, default=DEFAULT_CITY,
          required=True, notnull=True, label=T("Default City")),
    Field("default_state", length=STATE_SIZE, default=DEFAULT_STATE,
          required=True, notnull=True, label=T("Default Province/State")),
    Field("default_country", length=COUNTRY_SIZE, default=DEFAULT_COUNTRY,
          required=True, notnull=True, label=T("Default Country")),
    Field('registration_key', length=512,
          writable=False, readable=False, default=''),
    Field('reset_password_key', length=512,
          writable=False, readable=False, default=''),
    Field('registration_id', length=512,
          writable=False, readable=False, default=''))
custom_auth_table = db[auth.settings.table_user_name] # get the custom_auth_table
custom_auth_table.first_name.requires = \
  IS_NOT_EMPTY(error_message=auth.messages.is_empty)
custom_auth_table.last_name.requires = \
  IS_NOT_EMPTY(error_message=auth.messages.is_empty)
custom_auth_table.password.requires = [IS_STRONG(), CRYPT()]
custom_auth_table.email.requires = [
  IS_EMAIL(error_message=auth.messages.invalid_email),
  IS_NOT_IN_DB(db, custom_auth_table.email)]
auth.settings.table_user = custom_auth_table # tell auth to use custom_auth_table



crud = Crud(globals(),db)                      # for CRUD helpers using auth
service = Service(globals())                   # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()

mail.settings.server = 'logging' or 'smtp.gmail.com:587'  # your SMTP server
mail.settings.sender = 'you@gmail.com'         # your email
mail.settings.login = 'username:password'      # your credentials or None

auth.settings.hmac_key = 'sha512:77065033-112b-4415-a2b1-b3a79f5e480a'   # before define_tables()
auth.define_tables()                           # creates all needed tables
auth.settings.mailer = mail                    # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['reset_password'])+'/%(key)s to reset your password'

#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################

crud.settings.auth = None                      # =auth to enforce authorization on crud

